﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using System.Linq;
using UniMana.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmGroupList : DevExpress.XtraEditors.XtraForm
    {
        //public delegate void PassData(DataRow[] drSgi);
        int[] m_group;
        public delegate void PassData(DataTable dtSystem_User_Rule);
        public PassData passData;

        public Guid m_guiUserKey;
        DataTable m_dtSystem_User_Rule;
        public void DelegateGetRule(DataTable dt)
        {
            m_dtSystem_User_Rule = dt;
        }

        public FrmGroupList(Guid _guiUserKey, int[] group)
        {
            InitializeComponent();
            m_guiUserKey = _guiUserKey;
            m_group = group;
            LoadData();
        }

        public void LoadData()
        {
            
            DataTable dt = System_GroupData.GetGroupNotInUser(new DBMain(),m_guiUserKey).ToDataTable();
          
            for (int i = 0; i < m_group.Length; i++)
            {
                DataRow dRow = (from DataRow r in dt.Rows where r["System_GroupKey"].ToString().ToUpper() == m_group[i].ToString().ToUpper() select r).FirstOrDefault();
                if (dRow != null)
                    dt.Rows.Remove(dRow);
            }
            grcGroup.DataSource = dt;
        }

        private void grvGroup_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
           
        }

        private void grvGroup_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            tltFunction.DataSource = System_RuleData.RuleByGroup(new DBMain(), int.Parse(grvGroup.GetFocusedRowCellValue("System_GroupKey").ToString())).ToDataTable();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DataRow[] dr = new DataRow[grvGroup.SelectedRowsCount];
            int j = 0;
            foreach(int i in grvGroup.GetSelectedRows())
            {
                dr[j] = grvGroup.GetDataRow(i);
                //System_User_GroupInfo sugi = new System_User_GroupInfo(m_guiUserKey, int.Parse(dr[j][0].ToString()));
                //sugi.Save();
                j++;
            }

            FrmUserManager_SubSelectPermission f = new FrmUserManager_SubSelectPermission(dr, m_guiUserKey);
            f.passData = new FrmUserManager_SubSelectPermission.PassData(DelegateGetRule);
            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog();

            passData(m_dtSystem_User_Rule);

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

       
    }
}